<?php
/**
 * The class that handles the login/logout cookies.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */

/**
 * This class is a Singleton class, since only one instance
 * of this class is required.  This is achieved by making all
 * of the functions static.
 */
class loginInfo {

	static $username = NULL;
	static $password = NULL;

	const user_cookie = 'opol_user';
	const passwd_cookie = 'opol_passwd';

	const timeout = 1800;

	/**
	 * Update the current login.
	 */
	static function update_login () {
		if (!isset($_COOKIE[loginInfo::user_cookie])) {
			header("location:/?action=logout");
			return;
		}

		$username = $_COOKIE[loginInfo::user_cookie];
		$password = $_COOKIE[loginInfo::passwd_cookie];

		$t = time() + loginInfo::timeout;
		setcookie(loginInfo::user_cookie, $username, $t);
		setcookie(loginInfo::passwd_cookie, $password, $t);
	}

	/**
	 * Process a login.
	 */
	static function set_login ($username, $password) {
		$t = time() + loginInfo::timeout;

		setcookie(loginInfo::user_cookie, $username, $t);
		setcookie(loginInfo::passwd_cookie, $password, $t);
	}

	/**
	 * Process a logout.
	 */
	static function logout () {
		unset($_COOKIE[loginInfo::user_cookie]);
		unset($_COOKIE[loginInfo::passwd_cookie]);

		setcookie(loginInfo::user_cookie, NULL, -1);
		setcookie(loginInfo::passwd_cookie, NULL, -1);
	}
}
?>